<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>表格天气</title>
    <style>
        .container {
            width: 600px;
            height: 400px;
            margin: 200px auto 0;
            background-color: rgb(206, 211, 255);
            border-radius: 10px;
            text-align: center;
            padding: 20px
        }

        .search-group {
            margin-bottom: 50px;
        }

        table {
            width: 600px;
            margin: 0 auto;
        }
    </style>
</head>

<body>
    <div class="container">
        <div class="search-group">
            <input type="text" id="city">
            <button id='search'>查询</button>
        </div>
        <table border="1">
            <tbody>
                <thead>
                    <tr>
                        <th>日期</th>
                        <th>最低温度</th>
                        <th>最高温度</th>
                        <th>风力</th>
                        <th>风向</th>
                        <th>天气类型</th>
                    </tr>
                </thead>
            <tbody>
                <!-- <tr>
                    <td>6日星期四</td>
                    <td>低温 7℃</td>
                    <td>高温 12℃</td>
                    <td>3级</td>
                    <td>无持续风向</td>
                    <td>多云</td>
                </tr> -->
            </tbody>
            </tbody>
        </table>
    </div>
    <script>
        const btn = document.getElementById('search')
        const cityIpt = document.getElementById('city')
        const baseUrl = "http://wthrcdn.etouch.cn/weather_mini?city="
        const tbody = document.querySelector('tbody')

        //1. 给button绑定一个点击事件
        btn.onclick = function () {
            //2.获取input的值
            const city = cityIpt.value
            //3.拼接url
            const url = baseUrl + city
            //3.发送ajax请求
            const res = ajaxGet(url)
            
        }
        function ajaxGet(url) {
            //#1.创建xhr实例
            const xhr = new XMLHttpRequest()
            //#2.初始化一个请求
            xhr.open('GET', url)
            //#3.发送请求
            xhr.send()
            //#4.监听xhr的readystate的变化，使用onreadystatechange函数
            xhr.onreadystatechange = function (e) {
                if (xhr.readyState == 4 && xhr.status == 200) {
                    const res = JSON.parse(xhr.responseText)
                    // console.log(xhr.responseText)
                    console.log(res)
                    handleRes(res)
                }
            }
        }

        function handleRes(data){
            const forecastList = data.data.forecast
            const htmlArray = forecastList.map(item=>{
                return ` <tr>
                    <td>${item.date}</td>
                    <td>${item.low}</td>
                    <td>${item.high}</td>
                    <td>${extracFengli(item.fengli)}</td>
                    <td>${item.fengxiang}</td>
                    <td>${item.type}</td>
                </tr>`
            })
            const html = htmlArray.join('')
            tbody.innerHTML = html

        }

        function extracFengli(rowStr){
            // const reg = /<!\[CDATA\[(.*级)\]\]>/
            // const execRes = reg.exec(rowStr)
            // console.log(execRes)
            // if(execRes){
            //     return execRes[1]
            // } 
            // return ''
            return /<!\[CDATA\[(.*级)\]\]>/.exec(rowStr) ? /<!\[CDATA\[(.*级)\]\]>/.exec(rowStr)[1] : ''
        }
        
    </script>
</body>

</html>